<template>
  <div>
    <el-button type="text" @click="update">更新</el-button>
    <el-dialog title="更改商品信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="商品名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="单价" :label-width="formLabelWidth">
          <el-input v-model="form.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="销售总数" :label-width="formLabelWidth">
          <el-input type="number" v-model="form.total" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="出版社" :label-width="formLabelWidth">
          <el-input v-model="form.press" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库存量" :label-width="formLabelWidth">
          <el-input type="number" v-model="form.stock" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upfrom">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateGoods, getGoods, listgoods } from "@/api/data";

export default {
  data() {
    return {
      detailData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        stock: 0,
        press: "",
        total: 1,
        price: 0,
        name: ""
      },
      formLabelWidth: "120px"
    };
  },
  props: {
    id: {
      type: String,
      default: ""
    }
  },
  methods: {
    async update() {
      //点击的id
      console.log(this.id);

      this.dialogFormVisible = true;
      const ids = this.id;
      const datas = await getGoods(ids);
      this.detailData = datas;
      console.log(this.detailData);
      this.form = this.detailData;
    },
    upfrom() {
      this.dialogFormVisible = false;
      var date = new Date();
      var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
      var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
      var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
      var hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
      var minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
      var seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
      var currentDate =
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hours +
        ":" +
        minutes +
        ":" +
        seconds;
      //时间
      console.log(currentDate);
      //表单内容
      console.log(this.form);
      //请求
      const id = this.id;
      const data = this.form;
      delete data.id;
      delete data.createTime;
      updateGoods(id, data).then(data => {
        if (data) {
          console.log(data);
          this.getListgoods();
        }
      });
    },
    async getListgoods() {
      const data = await listgoods({ page: 1, perPage: 20 });
      const { list } = data;
      this.list = list || [];
      // console.log(this.list);
      this.$emit("upfrom", this.list);
    }
  }
};
</script>